package com.nbgroup.web;


import com.alibaba.fastjson.JSON;
import com.nbgroup.entity.*;
import com.nbgroup.entity.Identity.IdeantityAge;
import com.nbgroup.entity.Identity.Identity;
import com.nbgroup.entity.Identity.IdentityTest;
import com.nbgroup.entity.region.Province;
import com.nbgroup.service.Appraise.AppraiseService;

import com.nbgroup.service.DayVisitorsService;
import com.nbgroup.service.Identity.IdentityService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api")
public class ApiController extends BaseController {

    @Autowired
    AppraiseService appraiseService;
    @Autowired
    IdentityService identityService;
    @Autowired
    DayVisitorsService dayVisitorsService;

    /*
     *   获取未删除评价列表
     * @author 王玉涛
     * @Date 2018/9/3 10:44
     * @param
     * @return
     */
    @RequestMapping(value = "/getAppraises", method = RequestMethod.GET)
    public List<Appraise> getAppraises(Integer lid) {
        List<Appraise> appraises = appraiseService.getAppraises(0, lid);
        return appraises;
    }

    /*
     *
     * @author 王玉涛
     * @Date 2018/9/3 16:19
     * @param
     * @return
     */
    @RequestMapping(value = "/saveAppraise", method = RequestMethod.GET)
    public Integer saveAppraise(Appraise appraise) {
        Integer id = -1;
        try {

            id = appraiseService.saveAppraise(appraise).getId();
        } catch (Exception e) {
            e.printStackTrace();
            id = -1;
        }
        return id;
    }

    @RequestMapping(value = "/findPlatform", method = RequestMethod.GET)
    @ResponseBody
    public List<Platform> getPlatform() {
        List<Platform> platform = platformService.findPlatform();
        if (null != platform)
            return platform;
        else return null;
    }

    @RequestMapping(value = "/findExhibitionByPlatform", method = RequestMethod.GET)
    @ResponseBody
    public List<Exhibition> findExhibitionByPlatform(Integer icId) {
        System.out.println(icId);
        return exhibitionService.findExhibitionByPlatform(icId);
    }


    @RequestMapping(value = "/findExhibitionById", method = RequestMethod.GET)
    @ResponseBody
    public Exhibition findExhibitionById(String exId) {
        return exhibitionService.findExhibitionById(exId);
    }


    @RequestMapping(value = "/findLiteratureByExhibtionId", method = RequestMethod.GET)
    @ResponseBody
    public List<Literature> findLiteratureByExhibtionId(Integer exhibitionId) {
        return literatureService.findLiteratureByExhibtionId(exhibitionId);
    }

    @RequestMapping(value = "/findLiteratureById", method = RequestMethod.GET)
    @ResponseBody
    public Literature findLiteratureById(Integer literatureId) {
        return literatureService.findLiteratureById(literatureId);
    }


    @RequestMapping(value = "/findLiteratureListByExhibtionId", method = RequestMethod.GET)
    @ResponseBody
    public List<Literature> findLiteratureListByExhibtionId(Integer exhibitionId) {
        return literatureService.findLiteratureListByExhibtionId(exhibitionId);
    }

    @RequestMapping(value = "/findLiteratureChildById", method = RequestMethod.GET)
    @ResponseBody
    public List<LiteratureChild> findLiteratureChildById(Integer literId) {
        System.out.println(literId);
        return literatureChildService.findLiteratureChildById(literId);
    }

    /**
     * 身份证信息读取方法
     * 推送接口
     * Sure
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/GetIdcard")
    @ResponseBody
    public Map<String, Object> GetIdcard(@RequestBody(required = false) String request) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "00000000");
        map.put("message", "ok");
        if (request != null && !(request.equals("{\"sid\":\"databus.push.url.test\"}"))) {
            Logs logs = this.peopleMessage(request);
            this.addPeople(logs);
        }
        return map;
    }

    /**
     * 字符串切分
     * Sure
     *
     * @param request
     * @return
     */
    public Logs peopleMessage(String request) {
        Integer star = request.indexOf("[");
        Integer end = request.indexOf("]");
        String str = request.substring(star + 1, end);
        JSONObject jsonObject = JSONObject.fromObject(str);
        Logs logs = (Logs) JSONObject.toBean(jsonObject, Logs.class);
        System.out.println(logs);
        return logs;
    }

    /**
     * 新增游客
     *
     * @param logs
     * @return
     */
    public void addPeople(Logs logs) {
        String oneByIdentityId = identityService.findOneByIdentityId();
        if (!(oneByIdentityId.equals(logs.getIdentity_number()))) {
            Identity identity = new Identity();
            identity.setIdentityId(logs.getIdentity_number());
            identity.setAddress(logs.getAddress());
            Date date = null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
//                SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
            try {
                date = simpleDateFormat.parse(logs.getBirth());
                identity.setDateOfBirth(date);
                identity.setAgencyUnit(logs.getDepart());
                identity.setName(logs.getName());
                identity.setSex(logs.getSex());
                identity.setNation(logs.getNation());
                identity.setRecentDate(logs.getDate());
                identityService.addPeople(identity);
                dayVisitorsService.findTodayVisitorsAndSaves();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * app全国游客地区排行
     * 王硕
     * @param starTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/touristRank", method = RequestMethod.GET)
    public  Map<String, Object> touristRank(String starTime,String endTime) {
        List<Province> list = identityService.findProvinceCountByTime2(starTime,endTime);
        Map<String, Object> map = new HashMap<>();
        map.put("province",identityService.arrayProvince(list));
        map.put("count",identityService.arrayCount(list));
        return map;
    }

    @RequestMapping(value = "/touristRank1", method = RequestMethod.GET)
    public  List<Province> touristRank1(String starTime,String endTime) {
        System.out.println("__________________");
        List<Province> list = identityService.findProvinceCountByTime3(starTime,endTime);
        System.out.println("%%%%%%%%%%%%%%%%%%%%%%%");
        System.out.println("___"+list.get(1).getRegionName());

        return list;
    }

    /**
     * 火灾馆APP数据
     *
     * @param starTime
     * @param endTime
     * @return Lhl
     */
    @RequestMapping(value = "/findMansAll")
    public Map<String, Object> selectEventAll(String starTime,String endTime) {
        List<IdentityTest> list = identityService.testMans(starTime,endTime);
        System.out.println("##################");
        Map<String, Object> map = new HashMap<>();
        map.put("man",identityService.arrayProvince1(list));
        map.put("woman",identityService.arrayCount1(list));
        System.out.println("##################");
//        HelperClazz.renderJson(response, JSON.toJSONString(list));
        return map;
    }



    /**
     * 火灾馆APP数据年龄比例
     *
     * @param starTime
     * @param endTime
     * @return Lhl
     */
    @RequestMapping(value = "/ageRange")
    public Map<String, Object> age(String starTime,String endTime) {
        List<IdeantityAge> listage = identityService.ager(starTime,endTime);
        Map<String, Object> map = new HashMap<>();
        map.put("agerange",identityService.arrayProvince2(listage));
        return map;
    }
}
